package CSDN;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 王圆豪
 * Date: 2025-05-06
 * Time: 11:50
 */
class Counter{
    int count = 0;

    public synchronized void addCount(){
        count++;
    }
}

public class Test11 {
    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        Counter counter = new Counter();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.addCount();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.addCount();
            }
        });
        t1.start();
        t2.start();

        Thread.sleep(100);
        System.out.println(counter.count);
    }
}
